package com.hung.trade.user.mq.processer;

import com.alibaba.fastjson.JSON;
import com.hung.trade.common.constants.TradeEnums;
import com.hung.trade.common.exception.UserException;
import com.hung.trade.common.protocol.OrderCancelMessage;
import com.hung.trade.common.protocol.pay.ChangeUserMoneyReq;
import com.hung.trade.common.protocol.pay.ChangeUserMoneyResp;
import com.hung.trade.common.rocketmq.IMessageProcesser;
import com.hung.trade.user.service.IUserService;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;

/**
 * @author hungki
 * @createTime 2018-10-09 下午 2:18
 */
public class UserOrderCancelProcesser implements IMessageProcesser {
    private static final Logger logger = LoggerFactory.getLogger(UserOrderCancelProcesser.class);
    @Autowired
    private IUserService iUserService;
    @Override
    public boolean heandlerMessage(MessageExt messageExt) {
        try {
            String body =new String(messageExt.getBody(),"UTF-8");
            String topic = messageExt.getTopic();
            String msgId = messageExt.getMsgId();
            String keys = messageExt.getKeys();
            String tags = messageExt.getTags();
            logger.info(String.format("recive message [ body = %s ] ",body));

            OrderCancelMessage orderCancelMessage = JSON.parseObject(body,OrderCancelMessage.class);
            if(orderCancelMessage.getUserMoney()!=null || orderCancelMessage.getUserMoney().compareTo(BigDecimal.ZERO)==1){

                ChangeUserMoneyReq changeUserMoneyReq = new ChangeUserMoneyReq();
                changeUserMoneyReq.setMoneyLogType(orderCancelMessage.getMoneyLogType());
                changeUserMoneyReq.setUserMoney(orderCancelMessage.getUserMoney());
                changeUserMoneyReq.setOrderId(orderCancelMessage.getOrderId());
                changeUserMoneyReq.setUserId(orderCancelMessage.getUserId());
                ChangeUserMoneyResp changeUserMoneyResp = iUserService.changeUserMoney(changeUserMoneyReq);
                if(null == changeUserMoneyResp||changeUserMoneyResp.getRetCode().equals(TradeEnums.RetEnum.FAIL.getCode())){
                    logger.warn("修改用户余额操作，接口返回不成功，返回false，稍后重试");
                    if(messageExt.getReconsumeTimes()>3){
                        logger.warn("修改用户余额操作，接口返回不成功，重复三次仍然不成功，保存到数据库，返回true，稍后重试");
                        return true;
                    }
                    return false;
                }else{
                    logger.warn("修改用户余额操作，接口返回成功，返回true，标记消息消费成功");
                    return true;
                }
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return false;
        } catch (UserException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }
}
